var app = getApp();

const RongIMLib = app.globalData.RongIMLib;

var params = {}; 

Page({

    /**
     * 页面的初始数据
     */
    data: {
        data: [],
        page:1,              // 页码

        loading: false,      // 加载中
        noData: false,       // 没有数据
        noMore: false,       // 没有更多
        hint: false,         // 是否提示

        message_num:0,       // 未读消息条数
    },
    // 获取活动列表
    getAuctionList(){
        var self = this;
        var noData = false;                        // 是否没有数据
        var noMore = false;                        // 是否没有更多
        var page = this.data.page;
        var datas = this.data.data;            // 原数据

        wx.getStorage({
            key: 'user',
            success: function (res) {
                wx.request({
                    url: app.url.requestUrl + '/auction_manage/my_auction_list',
                    method: 'POST',
                    data: {
                        openid: res.data.openid,
                        token: res.data.token,
                        page: page
                    },
                    success: function (res) {
                        var list = res.data;
                        
                        if (list.code == 1) {
                            self.setData({
                                noData: !list.hasNext,
                                data: datas.concat(list.data.data),
                                loading: false
                            })
                        }
                        wx.hideLoading()
                    }
                })
            },
        })
    },
    getUserInfo(){
        var self = this;
        var url = "/personal/userinfo";

        wx.getStorage({
            key: 'user',
            success: function (res) {
                wx.request({
                    url: app.url.requestUrl + url,
                    method: 'POST',
                    data: {
                        openid: res.data.openid,
                        token: res.data.token,
                    },
                    success: function (res) {
                        var list = res.data;
                        if (list.code == 1) {
                            list.data.level = Math.floor(list.data.level);

                            res.data.data.score = app.globalData.levelRuleCount(res.data.data.score)

                            self.setData({
                                userInfo: list.data
                            })
                        }
                        wx.hideLoading()
                    }
                })
            },
        })
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
        this.getUserInfo();

        app.globalData.writeInContent = writeInContent;
        params.self = this;
    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function() {
        this.setData({
            data:[],
            page:1
        });
        
        this.getAuctionList()
        writeInContent();
    },
    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function() {
        var page = ++this.data.page;
        var noMore = this.data.noMore;
        var loading = this.data.loading;
        if (noMore && !loading) {
            this.setData({ page: page, loading: true })
            wx.showLoading({ title: '加载中' })
            this.getAuctionList();
        } else {
            if (!this.data.hint) {
                wx.showToast({
                    image: '../../img/warning.png',
                    title: '暂无更多',
                })
                this.setData({ hint: true })
            }
        }
    },
})

function writeInContent(){

    var conversationTypes = [RongIMLib.ConversationType.PRIVATE];
    RongIMLib.RongIMClient.getInstance().getConversationUnreadCount(conversationTypes, {
        onSuccess: function (count) {
            // count => 多个会话的总未读数
            params.self.setData({
                message_num: count
            })
        },
        onError: function (error) {
            // error => 获取多个会话未读数错误码
        }
    });

}